<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class addressController extends Controller
{
    
    public function getInsert(Request $request){
       // session(['id' => '32']);
       //  $ause_id = session('id');
        // dd($ause_id);
        if(!session('id')){
            return redirect('/login');
        }
        $data = $request->only(['aname','phone','posid','sheng','shi','xian','info']);
        // foreach ($ause_id as $k => $v) {
        $data['ause_id'] = session('id');
        // }
        // dd($data);
        // dd($data);

        $res = DB::table('addr')->insert($data);

        if($res){
            echo 1;
        }else{
            echo 2;
        }
    }

    public function getAddrlist()
    {
        if(!session('id')){
            return redirect('/login');
        }
        $ause_id = session('id');
        // $ause_id = 32;
        $menu = HomeListController::menu(0);
        $address = DB::table('addr')->where('ause_id',$ause_id)->get();
        // dd($address);
        // dd($address);
        return view('home.cart.addrlist',['address'=>$address,'menu'=>$menu]);
    }

// 地址编辑页面
    public function getAddredit($id)
    {
        $menu = HomeListController::menu(0);
        if(!session('id')){
            return redirect('/login');
        }
        // dd($id);
        // 从数据库中查找
        $address = DB::table('addr')->where('id',$id)->first();
        // dd($address);
        return view('home.cart.addredit',['address'=>$address,'menu'=>$menu]);
    }

    // 地址修改
    public function postAddrupdate(Request $request)
    {
        if(!session('id')){
            return redirect('/login');
        }
        $ause_id = session('id');
        $id = $request->only(['id']);
        // dd($id);
        // 接收要更新的数据
        $data = $request->only(['aname','phone','posid','sheng','shi','xian','info']);
        // dd($data);
        
        // 执行更新
        $res = DB::table('addr')->where('id',$id)->update($data);
        $address = DB::table('addr')->where('ause_id', $ause_id)->get();
        // dd($address);
        // dd($res);

        if($res){
            return redirect('/address/addrlist');
        }else{
            return back()->with('更新失败');
        }
    }

    // 执行删除
    public function getAddrdel(Request $request)
    {
        if(!session('id')){
            return redirect('/login');
        }
        $id = $request->input('id');
        $res = DB::table('addr')->where('id',$id)->delete();
        if($res){
            echo 1;
        }else{
            echo 2;
        }
    }

    // 设为默认地址
    public function getAddrdefault($id){
        if(!session('id')){
            return redirect('/login');
        }
        // dd($id);
        DB::table('addr')->update(['astatus' =>2]);
        $res = DB::table('addr')->where('id',$id)->update(['astatus'=>1]);
        if($res){
            
            return redirect('/address/addrlist');
        }else{
            alert("默认地址设置失败");
        }


    }
    
}
